#===============================================================================
# Koro Business Environment
# Copyright (C) 2008  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

'''
Created on 12/ago/2011

@author: antstud
'''

import wx
from koro.ui.wxgui.pages import BasePage,TablePage
from koro.ui.wxgui.widgets import controls as co
from koro.ui.wxgui.mellcfg import mDefaultBorder


#===============================================================================
# ListiniPage
#===============================================================================
class ListiniPage(TablePage):
    TITLE='Listini'
    def __init__(self,*args,**kwargs):
        TablePage.__init__(self,*args,**kwargs)
        #self._set_dialog_size((700,320))
        self._set_dialog_size((-1,-1))
        
    
    def _get_panel(self,*args,**kwargs):
        return ListiniPagePanel(*args,**kwargs)


#===============================================================================
# CodiciIvaPage
#===============================================================================
class CodiciIvaPage(TablePage):
    TITLE='Aliquote Iva'
    def __init__(self,*args,**kwargs):
        TablePage.__init__(self,*args,**kwargs)
        self._set_dialog_size((-1,-1))
    
    def _get_panel(self,*args,**kwargs):
        return CodiciIvaPagePanel(*args,**kwargs)


#===============================================================================
# ListiniPagePanel
#===============================================================================
class ListiniPagePanel(BasePage):
    TITLE='Listini'
    def __init__(self,*args,**kwargs):
        BasePage.__init__(self,*args,**kwargs)
        self.SetClientSize((700,300))
        self.nome = co.LabelTextCtrl(self, label='Nome', size=(200, -1),mstyle=co.mTE_REQUIRED,maxlen=20)
        self.descrizione = co.LabelTextCtrl(self,label='Note',size=(200, 55),mstyle=co.mTE_TITLED,style=wx.TE_MULTILINE)
        self.listino_formula = co.ListinoCtrl(self,allow_null=True)
        self.ricarico_formula = co.KPercentoCtrl(self,label='Ricarico',size=(100,-1))
        self.importo_fisso_formula = co.KValutaCtrl(self,label='Importo Fisso',size=(100,-1))
        
        self.__do_layout(args[2])        
        #self.list_ctrl.load(self._objects_list) 
        self.__bind()
        #self.list_ctrl.select(0,True)
        self.SetAutoLayout(True)
        #self.init_page_view()
    
    def __do_layout(self,objects):
        mainsizer=self.Sizer
        fieldsizer = wx.GridBagSizer(0,0)
        #vsizer=wx.BoxSizer(wx.VERTICAL)
        #lborder = 10
        #tborder = 6
        boxsizer=wx.BoxSizer(wx.VERTICAL)        
        
        boxsizer.Add(self.nome,0,wx.ALL,border=mDefaultBorder)

        boxsizer.Add(self.descrizione,0,wx.ALL,border=mDefaultBorder)
        
        boxsizer.AddSpacer(20)
        
        staticboxsizer=wx.StaticBoxSizer(wx.StaticBox(self,-1," Formula "),wx.VERTICAL)
        staticboxsizer.AddSpacer(8)
        
        staticboxsizer.Add(self.listino_formula,0,wx.ALL,border=mDefaultBorder)
        
        staticboxsizer.Add(self.ricarico_formula,0,wx.ALL,border=mDefaultBorder)
        
        staticboxsizer.Add(self.importo_fisso_formula,0,wx.ALL,border=mDefaultBorder)
        
        boxsizer.Add(staticboxsizer,0,wx.RIGHT|wx.BOTTOM,8)
        
        self.list_manager.set_list_size((400,320))
        self.set_list_columns(co.Column('Nome',140,'nome'),
                              #co.Column('Descrizione',250,'descrizione'),
                              co.Column('Formula',150,'get_formula_fmt'),
                              )
        self.load_list_objects(objects)
        
        fieldsizer.Add(boxsizer,(0,0),flag=wx.ALL|wx.EXPAND ,border=0)
        fieldsizer.Add(self.list_manager.get_managed_list(),(0,1),flag=wx.ALL|wx.EXPAND ,border=1)#riga zero colonna 1        
        fieldsizer.AddGrowableRow(0)
        fieldsizer.AddGrowableCol(1)
        
        mainsizer.Add(fieldsizer,1,flag=wx.ALL|wx.EXPAND ,border=10)
        mainsizer.Layout()
        
    def __bind(self):
        self.listino_formula.bind(wx.EVT_CHOICE,self._enable_formula)
    
    def _enable_formula(self,evt):
        listino=self.listino_formula.get_selected_object()
        enable=listino is not None
        self.ricarico_formula.enable(enable)
        self.importo_fisso_formula.enable(enable)
        if not enable:
            self.ricarico_formula.clear()
            self.importo_fisso_formula.clear()
    
    def enable_page(self,enable=True):
        self.list_manager.enable_list(enable)
        self.nome.enable(enable)
        self.listino_formula.enable(enable)
        self.descrizione.enable(enable)
        self.ricarico_formula.enable(enable)
        self.importo_fisso_formula.enable(enable)
            
    def update_fields(self,obj):
        print 'ListiniPage.update_fields'        
        self.nome.set_value(obj.nome)
        self.descrizione.set_value(obj.descrizione)
        self.listino_formula.set_value(obj.listino_formula)
        self.ricarico_formula.set_value(obj.ricarico_formula)
        self.importo_fisso_formula.set_value(obj.importo_fisso_formula)
        self._enable_formula(None)
    
    def reset_fields(self):
        print 'ListiniPage.reset_fields'
        self.nome.Clear()
        self.descrizione.Clear()
        self.listino_formula.clear()
        self.ricarico_formula.clear()
        self.importo_fisso_formula.clear()
    
    def reset_focus(self):
        self.nome.set_focus()
    
    def update_object(self,obj):
        print 'ListiniPage.update_object'
        obj.nome=self.nome.get_value()
        obj.descrizione=self.descrizione.get_value()
        obj.set_formula(self.listino_formula.get_selected_object(),
                        self.ricarico_formula.get_value(),
                        self.importo_fisso_formula.get_value()
                        )

#    def before_save(self):
#        return True
    
    def process_delete(self):
        if self._page_state!=BasePage.NORMAL_STATE:
            self.before_close()
            self.set_normal_state()#riporta la pagina in BasePage.NORMAL_STATE
        else:
            BasePage.process_delete(self)

    def after_save(self):
        self.listino_formula.refresh_listini()
        #self._update_prodotti()
        #new_cod=self.codice.GetValue()
        #self.update()
        #self.list_ctrl.select(self.list_ctrl.FindItem(0,new_cod))
        
    def after_delete(self):
        self.update()
        self.listino_formula.refresh_listini()


#===============================================================================
# CodiciIvaPagePanel
#===============================================================================
class CodiciIvaPagePanel(BasePage):
    TITLE="Aliquote Iva"
    def __init__(self,*args,**kwargs):
        BasePage.__init__(self,*args,**kwargs)        
        
        self.codice = co.LabelTextCtrl(self,
                                       label='Codice',
                                       size=(100,-1),
                                       mstyle=co.mTE_REQUIRED|co.mTE_UPPER,
                                       maxlen=10,
                                       orientation=wx.VERTICAL)
        self.aliquota = co.LabelTextCtrl(self,
                                        label='Aliquota',
                                        maxlen=50,
                                        size=(70, -1),
                                        mstyle=co.mTE_TITLED,
                                        orientation=wx.VERTICAL)
        self.descrizione = co.LabelTextCtrl(self,
                                            label='Descrizione',
                                            maxlen=50,
                                            size=(200, -1),
                                            mstyle=co.mTE_TITLED,
                                            orientation=wx.VERTICAL,
                                            #style=wx.TE_MULTILINE
                                            )
        
        self.__do_layout(args[2])                
        self.__bind()        
        self.SetAutoLayout(True)
        #self.init_page_view()
    
    def __do_layout(self,objects):
        
        fieldsizer = wx.GridBagSizer(0,0)
        
        boxsizer=wx.BoxSizer()
        #hsizer=wx.BoxSizer()
        boxsizer.Add(self.codice,0,wx.ALL,border=3)
        boxsizer.Add(self.aliquota,0,wx.ALL,border=3)
        #boxsizer.Add(hsizer,0,wx.ALL,1)
        boxsizer.Add(self.descrizione,0,wx.ALL,border=3)

        fieldsizer.Add(boxsizer,(0,0),flag=wx.ALL|wx.EXPAND|wx.GROW ,border=3)#riga zero colonna 1
        
        #self.list_manager.set_managed_list(co.KoroListCtrl(self,size=(350,400),edit=True))
        self.list_manager.set_list_size((370,350))
        self.set_list_columns(co.Column('Codice',100,'codice'),
                              co.Column('Aliquota',70,'aliquota'),
                              co.Column('Descrizione',200,'descrizione'),
                              )
        self.load_list_objects(objects)
        
        fieldsizer.Add(self.list_manager.get_managed_list(),(1,0),flag=wx.ALL|wx.EXPAND)
        fieldsizer.AddGrowableCol(1)
        fieldsizer.AddGrowableRow(0)
        self.Sizer.Add(fieldsizer,1,flag=wx.ALL|wx.EXPAND ,border=10)#riga zero colonna 1 
        #mainsizer.Layout()
    
    def __bind(self):
        pass
    
#    def process_delete(self):
#        if self._page_state!=BasePage.NORMAL_STATE:
#            self.before_close()
#        else:
#            BasePage.process_delete(self)
    def enable_page(self,enable=True):
        self.list_manager.enable_list(enable)
        self.codice.enable(enable)
        self.aliquota.enable(enable)
        self.descrizione.enable(enable)

    def update_fields(self,obj):
        print 'CodiciIvaPage.update_fields'
        self.codice.set_value(obj.get_codice())
        self.aliquota.set_value(obj.get_percentuale())
        self.descrizione.set_value(obj.get_descrizione())        

    def update_object(self,obj):
        print 'CodiciIvaPage.set_object'
        obj.set_codice(self.codice.get_value())
        obj.set_percentuale(self.aliquota.get_value())        
        obj.set_descrizione(self.descrizione.get_value())

    def reset_fields(self):
        print 'CodiciIvaPanel.reset_fields'
        self.codice.clear()
        self.aliquota.clear()        
        self.descrizione.clear()
    
    def reset_focus(self):
        self.codice.set_focus()

#    def after_save(self):        
#        self.update()
    
    def after_delete(self):
        self.update()